<?php
// +----------------------------------------------------------------------
// | sfadmin - Suifeng Administration
// +----------------------------------------------------------------------
// | Copyright (c) 2017-2020 http://suifeng.tech All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: suifeng <1787585212@qq.com>
// +----------------------------------------------------------------------

namespace app\admin\service;

class Config extends \suifeng\mvc\Service
{
    /**
     * 获取配置列表，支持缓存。
     *
     * @return array
     */
    public function get_all()
    {
        $list = cache('admin_config_service::get_all');

        if (!$list || APP_DEBUG) {
            $where['status'] = array('eq', 1);
            $order = 'sort asc, id asc';

            $rows = model('admin/config')->where($where)
                ->order($order)
                ->select();

            $list = array();
            foreach ($rows as $row) {
                if ($row['control'] == 'checkbox') {
                    $row['value'] = explode(',', $row['value']);
                } else if ($row['control'] == 'textarea' && $row['is_array']) {
                    $row['value'] = \suifeng\helper\StringHelper::parseAttr($row['value']);
                } else {
                    $row['value'] = $row['value'];
                }
                $list[$row['name']] = $row->getData();
            }

            cache('admin_config_service::get_all', $list);
        }
        return $list;
    }

    /**
     * 获取配置数组。
     *
     * @return unknown[]
     */
    public function get_array()
    {
        $list = cache('admin_config_service::get_array');
        if (!$list || APP_DEBUG) {
            $array = array();

            foreach ($this->get_all() as $row) {
                $array[$row['name']] = $row['value'];
            }

            cache('admin_config_service::get_array', $array);
        }
        return $array;
    }

    /**
     * 清除缓存。
     */
    public function clear_cache()
    {
        cache('admin_config_service::get_all', null);
        cache('admin_config_service::get_array', null);
    }

}
